package echat.future.cn.echat.my.utils;

import android.support.annotation.NonNull;
import android.view.View;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

/**
 * 防止抖动功能，将点击时间转换为可观察者
 */
public class RxUtils {
    public static Observable<View> clickView(@NonNull View view) {
        return Observable.create(new ViewClickOnSubscribe(view)).throttleFirst(2, TimeUnit.SECONDS);
    }

    public static class ViewClickOnSubscribe implements ObservableOnSubscribe<View>{

        public View mView;

        public ViewClickOnSubscribe(View view){
            this.mView = view;
        }

        @Override
        public void subscribe(final ObservableEmitter emitter) throws Exception {
            View.OnClickListener onClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    emitter.onNext(v);
                }
            };
            mView.setOnClickListener(onClickListener);
        }
    }
}
